package cc.rengu.igas.share.core.realize.impl;

import cc.rengu.igas.bsps.facade.base.Header;
import cc.rengu.igas.bsps.facade.bean.BindDevInfoBean;
import cc.rengu.igas.bsps.facade.bean.MchntBindInfoBean;
import cc.rengu.igas.bsps.facade.request.MchntBindListQueryRequset;
import cc.rengu.igas.bsps.facade.request.MchntUnBindRequest;
import cc.rengu.igas.bsps.facade.request.VoiceDevBindListQueryRequset;
import cc.rengu.igas.bsps.facade.request.VoiceDevBindOrUnBindRequest;
import cc.rengu.igas.bsps.facade.response.MchntBindListQueryResponse;
import cc.rengu.igas.bsps.facade.response.MchntUnBindResponse;
import cc.rengu.igas.bsps.facade.response.VoiceDevBindListQueryResponse;
import cc.rengu.igas.bsps.facade.response.VoiceDevBindOrUnBindResponse;
import cc.rengu.igas.bsps.facade.result.Result;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.ShareServiceConstant;
import cc.rengu.igas.share.common.enums.RespCodeEnum;
import cc.rengu.igas.share.core.model.DevBindInfo;
import cc.rengu.igas.share.core.model.MchntBindInfo;
import cc.rengu.igas.share.core.realize.BspsService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商户服务模块组件实现类
 * Created by wangcheng on 2021/6/18 18:10
 */
public class BspsServiceImpl implements BspsService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String callMode = "0";

    public BspsServiceImpl() throws Exception {
        String callMode = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.APPLICATION_CONF_NODE, AppConfigConstant.BSPS_SERVICE_CALL_MODE);
        if (!StringUtil.isEmptyOrNull(callMode)) {
            if (!"0".equals(callMode.trim()) && !"1".equals(callMode.trim())) {
                rglog.error("商户服务相关获取调用模式<{}>参数配置错误:0-本地调用，1-微服务调用!", callMode.trim());
                throw new BizException("商户服务相关信息获取方式配置有误:<{}>", callMode);
            } else {
                this.callMode = callMode.trim();
            }
        }
        rglog.info("商户服务相关信息获取方式为<{}>", "0".equals(callMode) ? "本地调用" : "微服务调用");
    }

    @Override
    public List<MchntBindInfo> getMchntBindInfo(String instId, String userId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String callSysId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SRC_SYS_ID);
        if (ChannelEnum.BOMP.getChannelType().equals(callSysId)) {
            rglog.error("运营管理模块获取商户服务相关信息时，请使用模块内的方法!!!!!!!!!!!!!");
            throw new BizException(RespCodeEnum.NOT_SUPPORT_BIZ.getRespCode(), RespCodeEnum.TRANS_NOT_SUPPORT.getRespDesc());
        }
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        rglog.info("正在获取商户<{}>绑定信息,信息获取方式为<{}>", mchntNo, "0".equals(callMode) ? "本地调用" : "微服务调用");
        Header header = new Header();
        header.setInstId(instId);
        header.setSrcSysId(ChannelEnum.BSPS.getChannelType());
        header.setChanlId(callSysId);
        header.setTxnNum("T00BS030");
        header.setBizType("00");
        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setTraceNo(sysSeqNum + RandomUtil.randomNumStr(4));
        header.setTransTime(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_TIME));
        header.setTransDate(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_DATE));
        MchntBindListQueryRequset mchntBindListQueryRequset = new MchntBindListQueryRequset();
        mchntBindListQueryRequset.setHeader(header);
        mchntBindListQueryRequset.setUserId(userId);
        mchntBindListQueryRequset.setMchntNo(mchntNo);
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntBindListQueryResponse> callResult;
        if ("0".equals(callMode)) {
            Class clasz = Class.forName(ShareServiceConstant.BSPS_MCHNT_QUERY_SERVICE_INNER_PATH);
            callResult = (Result<MchntBindListQueryResponse>) dubboService.callDubboInnerMethod(clasz, ShareServiceConstant.BSPS_GET_MCHNT_BIND_INFO_METHOD, mchntBindListQueryRequset);
        } else {
            callResult = (Result<MchntBindListQueryResponse>) dubboService.callDubboService(ShareServiceConstant.BSPS_MCHNT_QUERY_SERVICE, ShareServiceConstant.BSPS_GET_MCHNT_BIND_INFO_METHOD, mchntBindListQueryRequset);
        }
        if (null == callResult || null == callResult.getResult() || null == callResult.getResult().getMchntBindInfoBeanList()) {
            rglog.error("获取商户绑定信息失败，instId:<{}>,userId:<{}>,mchntNo:<{}>", instId, userId, mchntNo);
            return null;
        }
        List<MchntBindInfo> mchntBindInfoList = new ArrayList<>();
        for (MchntBindInfoBean mchntBindInfoBean : callResult.getResult().getMchntBindInfoBeanList()) {
            MchntBindInfo mchntBindInfo = new MchntBindInfo();
            BeanUtil.beanCopy(mchntBindInfoBean, mchntBindInfo);
            mchntBindInfoList.add(mchntBindInfo);
        }
        return mchntBindInfoList;
    }

    @Override
    public boolean mchntUnBind(String instId, String userId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        rglog.info("正在正在进行用户<{}>与商户<{}>的信息解绑,信息获取方式为<{}>", userId, mchntNo, "0".equals(callMode) ? "本地调用" : "微服务调用");
        Header header = new Header();
        header.setInstId(instId);
        header.setSrcSysId(ChannelEnum.BSPS.getChannelType());
        header.setChanlId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SRC_SYS_ID));
        header.setTxnNum("T00BS031");
        header.setBizType("00");
        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setTraceNo(sysSeqNum + RandomUtil.randomNumStr(4));
        header.setTransTime(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_TIME));
        header.setTransDate(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_DATE));
        MchntUnBindRequest mchntUnBindRequest = new MchntUnBindRequest();
        mchntUnBindRequest.setHeader(header);
        mchntUnBindRequest.setUserId(userId);
        mchntUnBindRequest.setMchntNo(mchntNo);
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntUnBindResponse> callResult;
        if ("0".equals(callMode)) {
            Class clasz = Class.forName(ShareServiceConstant.BSPS_MCHNT_MANAGE_SERVICE_INNER_PATH);
            callResult = (Result<MchntUnBindResponse>) dubboService.callDubboInnerMethod(clasz, ShareServiceConstant.BSPS_MCHNT_UNBIND_METHOD, mchntUnBindRequest);
        } else {
            callResult = (Result<MchntUnBindResponse>) dubboService.callDubboService(ShareServiceConstant.BSPS_MCHNT_MANAGE_SERVICE, ShareServiceConstant.BSPS_MCHNT_UNBIND_METHOD, mchntUnBindRequest);
        }
        if (null == callResult || null == callResult.getResult()) {
            rglog.error("商户解绑失败，instId:<{}>,userId:<{}>,mchntNo:<{}>", instId, userId, mchntNo);
            return false;
        }
        return callResult.getResult().getRespCode().equals(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
    }

    @Override
    public List<DevBindInfo> getDevBindInfo(String instId, String userId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        String callSysId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SRC_SYS_ID);
        if (ChannelEnum.BOMP.getChannelType().equals(callSysId)) {
            rglog.error("运营管理模块获取商户服务相关信息时，请使用模块内的方法!!!!!!!!!!!!!");
            throw new BizException(RespCodeEnum.NOT_SUPPORT_BIZ.getRespCode(), RespCodeEnum.TRANS_NOT_SUPPORT.getRespDesc());
        }
        rglog.info("正在获取用户<{}>在商户<{}>下的设备<{}>绑定信息,信息获取方式为<{}>", userId, mchntNo, "0".equals(callMode) ? "本地调用" : "微服务调用");
        Header header = new Header();
        header.setInstId(instId);
        header.setSrcSysId(ChannelEnum.BSPS.getChannelType());
        header.setChanlId(callSysId);
        header.setTxnNum("T00BS046");
        header.setBizType("00");
        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setTraceNo(sysSeqNum + RandomUtil.randomNumStr(4));
        header.setTransTime(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_TIME));
        header.setTransDate(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_DATE));
        VoiceDevBindListQueryRequset voiceDevBindListQueryRequset = new VoiceDevBindListQueryRequset();
        voiceDevBindListQueryRequset.setHeader(header);
        voiceDevBindListQueryRequset.setUserId(userId);
        voiceDevBindListQueryRequset.setMchntNo(mchntNo);
        DubboService dubboService = new DubboServiceImpl();
        Result<VoiceDevBindListQueryResponse> callResult;
        if ("0".equals(callMode)) {
            Class clasz = Class.forName(ShareServiceConstant.BSPS_MCHNT_QUERY_SERVICE_INNER_PATH);
            callResult = (Result<VoiceDevBindListQueryResponse>) dubboService.callDubboInnerMethod(clasz, ShareServiceConstant.BSPS_GET_DEV_BIND_INFO_METHOD, voiceDevBindListQueryRequset);
        } else {
            callResult = (Result<VoiceDevBindListQueryResponse>) dubboService.callDubboService(ShareServiceConstant.BSPS_MCHNT_QUERY_SERVICE, ShareServiceConstant.BSPS_GET_DEV_BIND_INFO_METHOD, voiceDevBindListQueryRequset);
        }
        if (null == callResult || null == callResult.getResult() || null == callResult.getResult().getBindDevInfoList()) {
            rglog.error("获取设备绑定信息失败，instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        List<DevBindInfo> devBindInfoList = new ArrayList<>();
        for (BindDevInfoBean bindDevInfoBean : callResult.getResult().getBindDevInfoList()) {
            DevBindInfo devBindInfo = new DevBindInfo();
            BeanUtil.beanCopy(bindDevInfoBean, devBindInfo);
            devBindInfoList.add(devBindInfo);
        }
        return devBindInfoList;
    }

    @Override
    public boolean devBindOrUnBind(String instId, String userId, String mchntNo, String oprType, Map<String, String> devInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        String callSysId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SRC_SYS_ID);
        if (ChannelEnum.BOMP.getChannelType().equals(callSysId)) {
            rglog.error("运营管理模块获取商户服务相关信息时，请使用模块内的方法!!!!!!!!!!!!!");
            throw new BizException(RespCodeEnum.NOT_SUPPORT_BIZ.getRespCode(), RespCodeEnum.TRANS_NOT_SUPPORT.getRespDesc());
        }
        rglog.info("正在正在进行用户<{}>在商户<{}>的设备绑定或解绑,信息获取方式为<{}>", userId, mchntNo, "0".equals(callMode) ? "本地调用" : "微服务调用");
        Header header = new Header();
        header.setInstId(instId);
        header.setSrcSysId(ChannelEnum.BSPS.getChannelType());
        header.setChanlId(callSysId);
        header.setTxnNum("T00BS047");
        header.setBizType("00");
        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setTraceNo(sysSeqNum + RandomUtil.randomNumStr(4));
        header.setTransTime(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_TIME));
        header.setTransDate(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_DATE));
        VoiceDevBindOrUnBindRequest voiceDevBindOrUnBindRequest = new VoiceDevBindOrUnBindRequest();
        voiceDevBindOrUnBindRequest.setHeader(header);
        voiceDevBindOrUnBindRequest.setUserId(userId);
        voiceDevBindOrUnBindRequest.setMchntNo(mchntNo);
        voiceDevBindOrUnBindRequest.setOprType(oprType);
        voiceDevBindOrUnBindRequest.setMsgPushFlag(devInfo.get("msgPushFlag"));
        voiceDevBindOrUnBindRequest.setDevType(devInfo.get("devType"));
        voiceDevBindOrUnBindRequest.setDevModel(devInfo.get("devModel"));
        voiceDevBindOrUnBindRequest.setProductId(devInfo.get("productId"));
        voiceDevBindOrUnBindRequest.setDevUniqueId(devInfo.get("devUniqueId"));
        voiceDevBindOrUnBindRequest.setDevOtherInfo(devInfo.get("devOtherInfo"));
        DubboService dubboService = new DubboServiceImpl();
        Result<VoiceDevBindOrUnBindResponse> callResult;
        if ("0".equals(callMode)) {
            Class clasz = Class.forName(ShareServiceConstant.BSPS_MCHNT_MANAGE_SERVICE_INNER_PATH);
            callResult = (Result<VoiceDevBindOrUnBindResponse>) dubboService.callDubboInnerMethod(clasz, ShareServiceConstant.BSPS_VOICE_DEV_BIND_OR_UNBIND_METHOD, voiceDevBindOrUnBindRequest);
        } else {
            callResult = (Result<VoiceDevBindOrUnBindResponse>) dubboService.callDubboService(ShareServiceConstant.BSPS_MCHNT_MANAGE_SERVICE, ShareServiceConstant.BSPS_VOICE_DEV_BIND_OR_UNBIND_METHOD, voiceDevBindOrUnBindRequest);
        }
        if (null == callResult || null == callResult.getResult()) {
            rglog.error("语音设备绑定或者解绑失败，instId:<{}>,userId:<{}>,mchntNo:<{}>,oprType:<{}>", instId, userId, mchntNo, oprType);
            return false;
        }
        return callResult.getResult().getRespCode().equals(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
    }
}
